package cn.kgc.controller;

import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.AppVersion;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.service.AppSercive;
import cn.kgc.util.PageDto;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppSercive appSercive;

    /**
     * 查询所有
     *
     * @param pageNumber
     * @param appInfo
     * @param session
     * @return
     */
    @RequestMapping("/queryAll")
    public String queryAllUser(@RequestParam(value = "pageNumber", defaultValue = "1") String pageNumber,
                               @RequestParam(value = "status", required = false, defaultValue = "7") String status,
                               AppInfo appInfo, HttpSession session) throws Exception {
        if ("1".equals(status)) {
            //待审核状态1  后台管理员登录
            appInfo.setStatus(Integer.parseInt(status));
            PageDto pageDto = appSercive.queryAll(Integer.parseInt(pageNumber), appInfo);
            if (pageDto != null) {
                session.setAttribute("pageDto", pageDto);
                return "backend/applist";
            }
            return "403";
        } else {
            //前台登录
            PageDto pageDto = appSercive.queryAll(Integer.parseInt(pageNumber), appInfo);
            if (pageDto != null) {
                session.setAttribute("pageDto", pageDto);
                return "developer/appinfolist";
            }
            return "403";
        }

    }

    /**
     * 所属平台ajax
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/queryflat")
    public void qf(HttpServletResponse response) throws Exception {
        String typeName = "所属平台";
        List<DataDictionary> dataDictionaryList = appSercive.listPlatform(typeName);
        System.out.println(dataDictionaryList.size() + "=======所属平台===========");
        response.getWriter().print(JSONArray.toJSON(dataDictionaryList));

    }

    /**
     * 一级分类ajax
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/queryLv1")
    public void lv1(HttpServletResponse response) throws Exception {
        List<AppCategory> list = appSercive.listLv1();
        System.out.println(list.size() + "=======一级分类ajax==============");
        response.getWriter().print(JSONArray.toJSON(list));

    }

    /**
     * 二三级分类
     *
     * @param id
     * @param response
     * @throws IOException
     */
    @RequestMapping("/queryLv2")
    public void lv2(@RequestParam String id, HttpServletResponse response) throws Exception {
        List<AppCategory> list = appSercive.listLv2(Integer.parseInt(id));
        System.out.println(list.size() + "=======2级分类ajax==============");
        response.getWriter().print(JSONArray.toJSON(list));
    }


    /**
     * 审核
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/check/{id}/{versionId}")
    public String appCheck(@PathVariable("id") String id, @PathVariable("versionId") String versionId, HttpSession session) throws Exception {
        //App基础信息
        List<AppInfo> list = appSercive.queryById(Integer.parseInt(id));
//        最新版本信息
        List<AppVersion> appList = appSercive.queryVersionById(Integer.parseInt(versionId));
        if (list != null && list.size() > 0) {
            session.setAttribute("appInfo", list);
            session.setAttribute("appVersion", appList);
            return "backend/appchecks";
        } else {
            return "403";
        }
    }

    /**
     * * 审核修改 审核通过，审核不通过  改变状态
     *
     * @param id
     * @param status
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/putaway1")
    public String updaStatus2(@RequestParam("id") String id, @RequestParam("status") String status
            , HttpServletRequest request) throws Exception {
        int i = appSercive.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        if (i > 0) {
            request.setAttribute("msg", 2);
//            return "backend/applist";
            return "redirect:/app/queryAll?status=1";
        } else {
            request.setAttribute("msg", 1);
//            return "backend/applist";
            return "redirect:/app/queryAll?status=1";
        }
    }

    /**
     * 上架  下架  修改info状态
     *
     * @param id
     * @param status
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/putaway/{id}/{status}")
    public String updaStatus(@PathVariable("id") String id, @PathVariable("status") String status
            , HttpServletRequest request) throws Exception {
        int i = appSercive.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        if (i > 0) {
            request.setAttribute("msg", 2);
//            return "developer/appinfolist";
            return "redirect:/app/queryAll";
        } else {
            request.setAttribute("msg", 1);
//            return "developer/appinfolist";
            return "redirect:/app/queryAll";
        }
    }

    /**
     * 查看
     *
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/showInfo")
    public String showy(@RequestParam("id") String id, HttpServletRequest request) throws Exception {
        //查询appp基本信息
        List<AppInfo> list = appSercive.queryById(Integer.parseInt(id));
        //查询历代版本
        List<AppVersion> appl = appSercive.appVersionL(Integer.parseInt(id));
        if (list != null && appl != null) {
            request.setAttribute("appInfoes", list);
            request.setAttribute("appVersionListes", appl);
            return "developer/appinfoview";
        } else {
            //暂无信息
            request.setAttribute("msg", 3);
            return "developer/appinfolist";
        }
    }

    /**
     * * 修改版本信息
     * 修改前先查找相应信息返回jsp
     * @return
     * @throws Exception
     */
    @RequestMapping("/modifys1")
    public String modi1(@RequestParam("vid") String id, @RequestParam("aid") String appId,
                        HttpServletRequest request) throws Exception {
        //查询历代版本
        List<AppVersion> appl = appSercive.appVersionL(Integer.parseInt(appId));
        if (appl != null && appl != null) {
            request.setAttribute("appVersionLists", appl);
            request.setAttribute("size",appl.size());
            return "developer/appversionmodify";
        } else {
            //暂无信息
            request.setAttribute("msg", 3);
            return "developer/appinfolist";
        }
    }

    @RequestMapping("/modifys")
    public String modi(AppVersion appVersion, HttpServletRequest request) throws Exception {
        int i = appSercive.modifies(appVersion);
        int i1=appSercive.updateVersionId(appVersion.getAppId(),appVersion.getId());
        if (i > 0) {
            //修改成功
            return "redirect:/app/queryAll";
        } else {
            return "redirect:/app/modifys1?id=" + appVersion.getId();
        }
    }

    @RequestMapping("/delVI")
    public String delVI(@RequestParam("vid")String id,@RequestParam("aid") String appId) throws Exception {
        int i = appSercive.deleteVI(Integer.parseInt(id));
        if (i > 0) {
            //修改成功
            return "redirect:/app/queryAll";
        } else {
            return "redirect:/app/modifys1?vid="+id+"&appId="+appId;
        }
    }
}